﻿package fj.ui
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import fj.events.FJKeyboardEvent;
	
	public class FJKeyboard extends MovieClip
	{
		public var effect:MovieClip;
		public var hist:MovieClip;
		
		public function FJKeyboard():void
		{
			effect.gotoAndStop( "hide" );
			hits.alpha = 0;
			hits.addEventListener( MouseEvent.CLICK, hitClick );
		}
		private function hitClick( e:MouseEvent ):void
		{
			var evento:FJKeyboardEvent;
			var tecla:String;
			//
			if ( e.target.name.length > 7 ) {		
				switch ( e.target.name.substr( -e.target.name.length+6,e.target.name.length+6 ) ) {
					case "virgula" :
						tecla = ",";
						break;
					case "ponto" :
						tecla = ".";
						break;
					case "traço" :
						tecla = "-";
						break;
					case "arroba" :
						tecla = "@";
						break;
					case "underscore" :
						tecla = "_";
						break;
					case "space" :
						tecla = " ";
						break;
					case "delete" :
						tecla = "delete";
						break;
					default :
						tecla="";
						break;
				}
			} else {
				tecla = e.target.name.substr( -1, 1 );
			}
			//dispatch event
			if (tecla!="delete") {
				evento = new FJKeyboardEvent( FJKeyboardEvent.INSERIR, tecla );
			} else {
				evento = new FJKeyboardEvent( FJKeyboardEvent.APAGAR );
			}
			dispatchEvent( evento );
			//effect
			effect.x = e.target.x;
			effect.y = e.target.y;
			effect.gotoAndPlay( "anim:start" );
			effect.addEventListener( Event.ENTER_FRAME, effectLoop );
		}
		private function effectLoop( e:Event ):void
		{
			if( effect.currentLabel == "anim:end" ){
				effect.gotoAndStop( "hide" );
				effect.removeEventListener( Event.ENTER_FRAME, effectLoop );
			}
		}
	}
}